<?php
/**
 * Created by PhpStorm.
 * User: Min
 * Date: 2018/4/18
 * Time: 14:42
 */

namespace SDXApi\Service;


use SDXApi\Model\SeminarTemplateTagGoodsModel;
use SDXApi\Model\SeminarTemplateTagModel;
use SDXApi\Repositories\GoodsRepository;

class SeminarTagGoodsService
{

    /**
     * @var SeminarTemplateTagModel
     */
    protected $tagModel;

    /**
     * @var SeminarTemplateTagGoodsModel
     */
    protected $tagGoodsModel;

    /**
     * @var GoodsRepository
     */
    protected $goodsRepository;

    /**
     * SeminarTagGoodsService constructor.
     */
    public function __construct()
    {
        $this->tagModel = new SeminarTemplateTagModel();
        $this->tagGoodsModel = new SeminarTemplateTagGoodsModel();
        $this->goodsRepository = new GoodsRepository();
    }

    /**
     * @author Min
     * @param array $template
     * @param int $userId
     * @param int $wid
     * @return array
     */
    public function handle($template = array(), $userId = 0, $wid = 8)
    {
        return $this->tagGoods($template, $userId, $wid);
    }

    /**
     * @author Min
     * @param array $page
     * @param int $userId
     * @param int $wid
     * @return array
     */
    protected function tagGoods($page = array(), $userId = 0, $wid = 8)
    {
        $this->goodsRepository->userId = $userId;
        $this->goodsRepository->wid = $wid;

        $tagList = $this->tagModel->getTagList($page['id']);

        /*foreach ($tagList as $key => $val) {
            //$tagList[$key]['goods'] = $this->handleGoods($val['tag_id'], $wid);
        }*/

        $page['tag_background_color'] = $tagList[0]['tag_background_color'];
        $page['tag_default_color'] = $tagList[0]['tag_default_color'];
        $page['tag_selected_color'] = $tagList[0]['tag_selected_color'];

        $page['tag_list'] = $tagList;

        unset($page['font_type'], $page['font_size'], $page['font_content'], $page['font_link'], $page['image_url'], $page['image_link'], $page['image_size'], $page['template_id'], $page['goods_id']);

        return $page;
    }

    /**
     * @author Min
     * @param $tag_id
     * @param $wid
     * @return array
     */
    protected function handleGoods($tag_id, $wid)
    {
        $goods = $this->tagGoodsModel->getTagGoodsList($tag_id, $wid);

        $goodsData = $this->goodsRepository->formatGoodsTransform($goods);

        return $this->goodsRepository->checkGoodsPractical($goodsData);
    }
}